<!-- file flowdet -->
<script> 
	
	$(function(){
	
		/***********************************************
		
			field.datepicker({ dateFormat : 'dd-mm-yy'});
			field.getCombobox({ task : "task"},"id","name");

		************************************************/
		
	
		/* REMOVE DUPLICATE ID */
		var ids = [];
		$('*').each(function() {
		    if (this.id && this.id !== '') {
		        if (ids[this.id]) {
		            $(this).remove();
		        } else {
		            ids[this.id] = this
		        }
		    }
		});
		
		$.fn.getCombobox = function(data, key, label){
			var field = $(this);
			$.ajax({
				url : "c_public_function",
				data : data,
				type : 'POST', dataType : 'json'
			}).done(function(response){
				for(i=0;i<response.length;i++){
					field.append("<option value="+response[i][key]+">"+response[i][label]+"</option>");
				}
					field.chosen();
			});
		}
		
		/* external function */
		
		$.fn.serializeObject = function()
		{
		   var o = {};
		   var a = this.serializeArray();
		   $.each(a, function() {
			   if (o[this.name]) {
				   if (!o[this.name].push) {
					   o[this.name] = [o[this.name]];
				   }
				   o[this.name].push(this.value || '');
			   } else {
				   o[this.name] = this.value || '';
			   }
		   });
		   return o;
		};
		
		/* declarisasi */
		
		task_flowdet = $("#task_flowdet");
		grid_flowdet = $("#grid-flowdet");
		pgrid_flowdet = $("#pgrid-flowdet");
		btnadd_flowdet = $("#btn-add-flowdet");
		btnedit_flowdet = $("#btn-edit-flowdet");
		btndelete_flowdet = $("#btn-delete-flowdet");
		btncancel_flowdet =  $("#btn-cancel-flowdet");
		btnsave_flowdet = $("#btn-save-flowdet");
		form_flowdet = $("#form-flowdet");
		
		field_flowdet_id = $("#flowdet_id");
		field_flowdet_seq = $("#flowdet_seq");
		field_m_flow_id = $("#m_flow_id");
		field_m_div_id = $("#m_div_id");
		field_s_users_id = $("#s_users_id");
		
		/* method started */
		
		$( "input[type=submit],input[type=button],input[type=reset], button" ).button();
			
		/* make component */
		
		grid_flowdet.jqGrid({ 
			ajaxGridOptions : {type:"POST"},
			serializeGridData : function(postdata) {return postdata; },
			postData : {task : 'LIST'},
            url: 'c_flowdet',
			datatype: "json", 
			colNames:[
				'flowdet_seq',
				'm_flow_id',
				'm_div_id',
				's_users_id',
				'flowdet_id'],
			colModel:[
				{ name: 'flowdet_seq'  ,index: 'flowdet_seq' , search : true},
				{ name: 'm_flow_id'  ,index: 'm_flow_id' , search : true},
				{ name: 'm_div_id'  ,index: 'm_div_id' , search : true},
				{ name: 's_users_id'  ,index: 's_users_id' , search : true},
				{ name: 'flowdet_id'  ,index: 'flowdet_id',key: true, hidden: true , search : false}
			],
			width: '700',
			height: 'auto',
			rowNum:10, 
			rowList:[10,20,30], 
			pager: '#pgrid-flowdet', 
			sortname: 'flowdet_id', 
			viewrecords: true, 
			sortorder: "desc",
			caption:"" 
		}); 
		grid_flowdet.jqGrid('navGrid','#pgrid-flowdet',{edit:false,add:false,del:false,search:false});
		grid_flowdet.jqGrid('filterToolbar',{});
		
		form_flowdet.dialog({
			autoOpen: false,
			height: "auto",
			width: "auto",
			modal: true
		});
		
		/* event */

		btnedit_flowdet.click(function(){
			id = grid_flowdet.jqGrid("getGridParam","selrow");
			if (id != null){
				task_flowdet.val("UPDATE");
				dataRow = grid_flowdet.getRowData(id);
				field_flowdet_id.val(dataRow.flowdet_id);
				field_flowdet_seq.val(dataRow.flowdet_seq);
				field_m_flow_id.val(dataRow.m_flow_id);
				field_m_div_id.val(dataRow.m_div_id);
				field_s_users_id.val(dataRow.s_users_id);
				form_flowdet.dialog("open");
			}
		});
		
		btndelete_flowdet.click(function(){
			id = grid_flowdet.jqGrid("getGridParam","selrow");
			if (id != null){
				confr = confirm("Apa Anda yakin menghapus data ini?");
				if (confr){
					$.ajax({
						type: 'POST',
						url: 'c_flowdet',
						data: {flowdet_id: id, task : 'DELETE'},
						success: function(response){
							if(response==1){
								grid_flowdet.trigger('reloadGrid');
								$("#grid-flowdet").trigger('reloadGrid');
							}
						}
					});
				}
			}
		});
		
		btnadd_flowdet.click(function(){
			task_flowdet.val("INSERT");
			form_flowdet.dialog("open");
		});
		
		btncancel_flowdet.click(function(){
			form_flowdet.dialog("close");
		});
		
		btnsave_flowdet.click(function(){
			data = form_flowdet.serializeObject();
			$.ajax({
				url : "c_flowdet",
				data : data,
				type : 'POST'
			}).done(function(response){
				grid_flowdet.trigger('reloadGrid');
				btncancel_flowdet.click();
			});
		});
		
	});
</script>

	<table id="grid-flowdet" class="table table-striped table-bordered" ></table>
	<div id="pgrid-flowdet"></div>
	
	<button id="btn-add-flowdet">Tambah</button>
	<button id="btn-edit-flowdet">Ubah</button>
	<button id="btn-delete-flowdet">Hapus</button>
	
	<form id="form-flowdet" title="flowdet" disabled=true>
		<input type="hidden" value="INSERT" name="task" id="task_flowdet">
	   <table>
			<tr style="display: none">
				<td align="right">flowdet_id:</td>
				<td align="left"><input id="flowdet_id" name="flowdet_id" /></td>
			</tr>
			
			<tr > <td align="right">flowdet_seq:</td> <td align="left"><input id="flowdet_seq" name="flowdet_seq" /></td> </tr>
			<tr > <td align="right">m_flow_id:</td> <td align="left"><input id="m_flow_id" name="m_flow_id" /></td> </tr>
			<tr > <td align="right">m_div_id:</td> <td align="left"><input id="m_div_id" name="m_div_id" /></td> </tr>
			<tr > <td align="right">s_users_id:</td> <td align="left"><input id="s_users_id" name="s_users_id" /></td> </tr>
			
			<tr>
				<td align="right"></td>
				<td style="padding-top: 10px;" align="right">
					<input style="margin-right: 5px;" type="button" id="btn-save-flowdet" value="Simpan"  />
					<input id="btn-cancel-flowdet" type="reset" value="Batal" /></td>
			</tr>
		</table>
	</form>